using UnityEngine;
using System;
using System.Collections.Generic;

/// <summary>
/// 日志级别枚举
/// </summary>
public enum LogLevel
{
    None = 0,
    Error = 1,
    Warning = 2,
    Info = 3,
    Debug = 4,
    All = 5
}

/// <summary>
/// 统一日志管理器 - 封装Unity的Debug日志系统，提供更灵活的日志管理功能
/// </summary>
public static class Logger
{
    private static GameConfig m_GameConfig;
    private static LogLevel m_CurrentLogLevel = LogLevel.All;
    private static bool m_EnableStackTrace = false;
    private static Dictionary<string, bool> m_TagFilters = new Dictionary<string, bool>();
    private static bool m_IsInitialized = false;
    
    /// <summary>
    /// 配置文件资源路径
    /// </summary>
    private const string CONFIG_RESOURCE_PATH = "Configs/GameConfig";
    
    /// <summary>
    /// 初始化Logger，从配置文件中加载设置
    /// </summary>
    public static void Initialize()
    {
        if (m_IsInitialized) return;
        
        LoadConfig();
        m_IsInitialized = true;
    }
    
    /// <summary>
    /// 从Resources加载配置文件
    /// </summary>
    private static void LoadConfig()
    {
        m_GameConfig = Resources.Load<GameConfig>(CONFIG_RESOURCE_PATH);
        
        if (m_GameConfig != null)
        {
            m_CurrentLogLevel = m_GameConfig.LogLevel;
            m_EnableStackTrace = m_GameConfig.EnableStackTrace;
            Debug.Log($"[Logger] 已从配置文件加载日志设置: LogLevel={m_CurrentLogLevel}, EnableStackTrace={m_EnableStackTrace}");
        }
        else
        {
            Debug.LogWarning($"[Logger] 未找到配置文件 {CONFIG_RESOURCE_PATH}，使用默认设置: LogLevel={m_CurrentLogLevel}");
        }
    }
    
    /// <summary>
    /// 设置配置文件（运行时动态设置）
    /// </summary>
    public static void SetConfig(GameConfig config)
    {
        m_GameConfig = config;
        if (config != null)
        {
            m_CurrentLogLevel = config.LogLevel;
            m_EnableStackTrace = config.EnableStackTrace;
        }
    }
    
    /// <summary>
    /// 获取当前使用的配置文件
    /// </summary>
    public static GameConfig GetConfig()
    {
        return m_GameConfig;
    }
    
    /// <summary>
    /// 当前日志级别
    /// </summary>
    public static LogLevel CurrentLogLevel
    {
        get
        {
            // 如果未初始化，尝试初始化
            if (!m_IsInitialized)
            {
                Initialize();
            }
            return m_CurrentLogLevel;
        }
        set => m_CurrentLogLevel = value;
    }
    
    /// <summary>
    /// 是否启用堆栈跟踪
    /// </summary>
    public static bool EnableStackTrace
    {
        get
        {
            // 如果未初始化，尝试初始化
            if (!m_IsInitialized)
            {
                Initialize();
            }
            return m_EnableStackTrace;
        }
        set => m_EnableStackTrace = value;
    }
    
    /// <summary>
    /// 设置标签过滤（允许或禁止特定标签的日志）
    /// </summary>
    public static void SetTagFilter(string tag, bool enabled)
    {
        m_TagFilters[tag] = enabled;
    }
    
    /// <summary>
    /// 清除所有标签过滤
    /// </summary>
    public static void ClearTagFilters()
    {
        m_TagFilters.Clear();
    }
    
    /// <summary>
    /// 检查标签是否被过滤
    /// </summary>
    private static bool IsTagFiltered(string tag)
    {
        if (string.IsNullOrEmpty(tag) || m_TagFilters.Count == 0)
            return false;
            
        if (m_TagFilters.TryGetValue(tag, out bool enabled))
            return !enabled;
            
        return false;
    }
    
    /// <summary>
    /// 普通日志输出
    /// </summary>
    public static void Log(object message)
    {
        if (m_CurrentLogLevel < LogLevel.Info)
            return;
            
        Debug.Log(message);
    }
    
    /// <summary>
    /// 带标签的普通日志输出
    /// </summary>
    public static void Log(string tag, object message)
    {
        if (m_CurrentLogLevel < LogLevel.Info)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.Log($"[{tag}] {message}");
    }
    
    /// <summary>
    /// 格式化日志输出
    /// </summary>
    public static void LogFormat(string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Info)
            return;
            
        Debug.LogFormat(format, args);
    }
    
    /// <summary>
    /// 带标签的格式化日志输出
    /// </summary>
    public static void LogFormat(string tag, string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Info)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogFormat($"[{tag}] {format}", args);
    }
    
    /// <summary>
    /// 警告日志输出
    /// </summary>
    public static void LogWarning(object message)
    {
        if (m_CurrentLogLevel < LogLevel.Warning)
            return;
            
        Debug.LogWarning(message);
    }
    
    /// <summary>
    /// 带标签的警告日志输出
    /// </summary>
    public static void LogWarning(string tag, object message)
    {
        if (m_CurrentLogLevel < LogLevel.Warning)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogWarning($"[{tag}] {message}");
    }
    
    /// <summary>
    /// 格式化警告日志输出
    /// </summary>
    public static void LogWarningFormat(string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Warning)
            return;
            
        Debug.LogWarningFormat(format, args);
    }
    
    /// <summary>
    /// 带标签的格式化警告日志输出
    /// </summary>
    public static void LogWarningFormat(string tag, string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Warning)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogWarningFormat($"[{tag}] {format}", args);
    }
    
    /// <summary>
    /// 错误日志输出
    /// </summary>
    public static void LogError(object message)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        Debug.LogError(message);
    }
    
    /// <summary>
    /// 带标签的错误日志输出
    /// </summary>
    public static void LogError(string tag, object message)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogError($"[{tag}] {message}");
    }
    
    /// <summary>
    /// 格式化错误日志输出
    /// </summary>
    public static void LogErrorFormat(string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        Debug.LogErrorFormat(format, args);
    }
    
    /// <summary>
    /// 带标签的格式化错误日志输出
    /// </summary>
    public static void LogErrorFormat(string tag, string format, params object[] args)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogErrorFormat($"[{tag}] {format}", args);
    }
    
    /// <summary>
    /// 异常日志输出
    /// </summary>
    public static void LogException(Exception exception)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        Debug.LogException(exception);
    }
    
    /// <summary>
    /// 带标签的异常日志输出
    /// </summary>
    public static void LogException(string tag, Exception exception)
    {
        if (m_CurrentLogLevel < LogLevel.Error)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        Debug.LogError($"[{tag}] Exception: {exception.Message}");
        Debug.LogException(exception);
    }
    
    /// <summary>
    /// Debug级别日志（仅在Debug模式下输出）
    /// </summary>
    public static void LogDebug(object message)
    {
        if (m_CurrentLogLevel < LogLevel.Debug)
            return;
            
        #if UNITY_EDITOR || DEVELOPMENT_BUILD
        Debug.Log($"[DEBUG] {message}");
        #endif
    }
    
    /// <summary>
    /// 带标签的Debug级别日志
    /// </summary>
    public static void LogDebug(string tag, object message)
    {
        if (m_CurrentLogLevel < LogLevel.Debug)
            return;
            
        if (IsTagFiltered(tag))
            return;
            
        #if UNITY_EDITOR || DEVELOPMENT_BUILD
        Debug.Log($"[DEBUG][{tag}] {message}");
        #endif
    }
}

